using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using Code5Systems.FormMaster.Business;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace Code5Systems.FormMaster
{

	public partial class FormMasterFormOptions : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		
#region  Web Form Designer Generated Code
		
		//This call is required by the Web Form Designer.
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.CheckBox chkSaveData;
		protected System.Web.UI.WebControls.TextBox txtTableClass;
		protected System.Web.UI.WebControls.DropDownList lstEmailSendAction;
		protected System.Web.UI.WebControls.TextBox txtEmailSendTo;
		protected System.Web.UI.WebControls.DropDownList lstEmailSendToControl;
		protected System.Web.UI.WebControls.TextBox txtFrom;
		protected System.Web.UI.WebControls.TextBox txtBCC;
		protected System.Web.UI.WebControls.DropDownList lstEmailFromAction;
		protected System.Web.UI.WebControls.DropDownList lstEmailSendFromControl;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvEmailSendTo;
		protected DotNetNuke.UI.UserControls.LabelControl lblToEmailText;
		protected DotNetNuke.UI.UserControls.LabelControl lblToEmailList;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvFrom;
		protected System.Web.UI.WebControls.RegularExpressionValidator revFrom;
		protected DotNetNuke.UI.UserControls.LabelControl lstFromEmailText;
		protected DotNetNuke.UI.UserControls.LabelControl lstFromEmailList;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvFormName;
		protected System.Web.UI.WebControls.RegularExpressionValidator revBCC;
		protected System.Web.UI.WebControls.Label lblRequired;
		protected System.Web.UI.WebControls.DropDownList lstCCMeEmail;
		protected System.Web.UI.WebControls.CheckBox chkShowCCMe;
		protected System.Web.UI.WebControls.TextBox txtFormName;
		protected System.Web.UI.WebControls.CheckBox chkUseCaptcha;
		protected System.Web.UI.WebControls.TextBox txtCaptchaCharacters;
		protected DropDownList lstReplyToAction;
		protected DotNetNuke.UI.UserControls.LabelControl lblReplyToEmailText;
		protected DotNetNuke.UI.UserControls.LabelControl lblReplyToEmailList;
		protected TextBox txtReplyTo;
		protected DropDownList lstEmailReplyToControl;
		protected RequiredFieldValidator rfvReplyTo;
		protected RegularExpressionValidator revReplyTo;
		
		//NOTE: The following placeholder declaration is required by the Web Form Designer.
		//Do not delete or move it.
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: This method call is required by the Web Form Designer
			//Do not modify it using the code editor.
			InitializeComponent();
		}
		
#endregion
		
		
#region Private Members
		
		private DotNetNuke.Entities.Modules.PortalModuleBase _loaderCtl;
		private string strOperationSuccess;
		private FormSettings objFormSettings;
		private Code5Systems.FormMaster.Business.FormController objFormCtl;
		private Code5Systems.FormMaster.Business.FormInfo objFormInfo;
		//Private objFormXmlDoc As XmlDocument
		private XDocument xDoc;
		
#endregion
		
		
#region Event Handlers
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			try
			{
				this.LoaderCtl = (PortalModuleBase) this.Parent.Parent;
				
				strOperationSuccess = Localization.GetString("OperationSuccess", this.LocalResourceFile);
				
				objFormSettings = Utility.GetFormSettings(ModuleId);
				objFormCtl = new Code5Systems.FormMaster.Business.FormController(ModuleId);
				objFormInfo = objFormCtl.FormInfo;
				
				if (objFormInfo.FormDefinition == string.Empty)
				{
					
					xDoc = XDocument.Load(Server.MapPath(this.ControlPath + "assets/xml/BaseForm.xml"));
					objFormCtl.UpdateFormDefinition(ModuleId, xDoc.ToString(), this.UserId);
					objFormInfo.FormDefinition = xDoc.ToString();
					objFormCtl.Update(objFormInfo);
				}
				else
				{
					xDoc = XDocument.Parse(objFormInfo.FormDefinition);
				}
				
				if (!Page.IsPostBack)
				{
					BindData();
				}
				
				if ((!Request.UserAgent.Contains("MSIE")) || Request.Browser.Platform.Contains("Mac"))
				{
					if (DotNetNuke.Framework.AJAX.IsInstalled())
					{
						Utility.DisableAjax(this);
					}
				}
				
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		
		//Update for the main form values, not the individulal Form Controls
		protected void cmdUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				if (Page.IsValid)
				{
					bool bNewForm = false;
					
					objFormInfo.LastModifiedByID = this.UserId;
					
					objFormInfo.FormName = this.txtFormName.Text;
					objFormInfo.FromAddress = this.txtFrom.Text;
					objFormInfo.BccAddress = this.txtBCC.Text;
					objFormInfo.TableClass = this.txtTableClass.Text;
					objFormInfo.EmailSendAction = Convert.ToByte(this.lstEmailSendAction.SelectedValue);
					objFormInfo.EmailFromAction = Convert.ToByte(this.lstEmailFromAction.SelectedValue);
					objFormInfo.ReplyToAction = Convert.ToByte(this.lstReplyToAction.SelectedValue);

					if ((FormInfo.EmailActionNone)objFormInfo.EmailSendAction == Code5Systems.FormMaster.Business.FormInfo.EmailActionNone.UserDefined)
					{
						string[] strAryEmails = this.txtEmailSendTo.Text.Replace(" ", "").Split(';');
						foreach (string email in strAryEmails)
						{
							if (!Utility.ValidateEmailAddressFormat(email))
							{
								throw (new ArgumentException(email + " " + Localization.GetString("InvalidEmail", this.LocalResourceFile)));
							}
						}
					}
					
					objFormInfo.EmailSendTo = this.txtEmailSendTo.Text;
					objFormInfo.EmailSendToControl = this.lstEmailSendToControl.SelectedValue;
					objFormInfo.EmailSendFromControl = this.lstEmailSendFromControl.SelectedValue;
					objFormInfo.ReplyTo = this.txtReplyTo.Text;
					objFormInfo.ReplyToControl = this.lstEmailReplyToControl.SelectedValue;
					objFormInfo.SaveData = this.chkSaveData.Checked;
					objFormInfo.ShowCcMe = this.chkShowCCMe.Checked;
					objFormInfo.EmailCcMeControl = this.lstCCMeEmail.SelectedValue;
					objFormInfo.UseCaptcha = this.chkUseCaptcha.Checked;
					objFormInfo.CaptchaCharacters = this.txtCaptchaCharacters.Text;
					objFormInfo.FormDefinition = xDoc.ToString();
					
					objFormCtl.Update(objFormInfo);
					
					DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
					
				}
			}
			catch (ArgumentException exArg)
			{
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, exArg.GetBaseException().Message, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		
		//DropdownList Send Action OnChange Event
		protected void lstEmailSendAction_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			SetSendToControls();
		}
		
		//DropdownList From Action OnChange Event
		protected void lstEmailFromAction_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			SetSendFromControls();
		}
		
		protected void lstReplyTo_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			this.SetReplyToControls();
		}
		
		protected void chkShowCCMe_CheckedChanged(System.Object sender, System.EventArgs e)
		{
			this.lstCCMeEmail.Enabled = this.chkShowCCMe.Checked;
		}
		
#endregion
		
		
#region DataBind Methods
		
		//Bind the Design Time Form controls
		private void BindData()
		{
			
			if (objFormInfo == null)
			{
				objFormInfo = new Code5Systems.FormMaster.Business.FormInfo();
				ViewState.Add("NewForm", "True");
			}
			
			this.txtFormName.Text = objFormInfo.FormName;
			this.txtTableClass.Text = objFormInfo.TableClass;
			this.lstEmailSendAction.SelectedIndex = this.lstEmailSendAction.Items.IndexOf(this.lstEmailSendAction.Items.FindByValue(objFormInfo.EmailSendAction.ToString()));
			this.lstEmailFromAction.SelectedIndex = this.lstEmailFromAction.Items.IndexOf(this.lstEmailFromAction.Items.FindByValue(objFormInfo.EmailFromAction.ToString()));
			this.lstReplyToAction.SelectedIndex = this.lstReplyToAction.Items.IndexOf(this.lstReplyToAction.Items.FindByValue((objFormInfo.ReplyToAction).ToString()));
			this.txtEmailSendTo.Text = objFormInfo.EmailSendTo;
			this.txtReplyTo.Text = objFormInfo.ReplyTo;
			
			this.chkSaveData.Checked = objFormInfo.SaveData;
			
			this.chkUseCaptcha.Checked = objFormInfo.UseCaptcha;
			this.txtCaptchaCharacters.Text = objFormInfo.CaptchaCharacters;
			
			if (objFormInfo.FromAddress == string.Empty)
			{
				this.txtFrom.Text = this.PortalSettings.Email;
			}
			else
			{
				this.txtFrom.Text = objFormInfo.FromAddress;
			}
			
			this.txtBCC.Text = objFormInfo.BccAddress;
			
			this.chkShowCCMe.Checked = objFormInfo.ShowCcMe;
			objFormCtl.BindDynamicControls(this.lstCCMeEmail, ModuleId);
			this.lstCCMeEmail.Enabled = objFormInfo.ShowCcMe;
			this.lstCCMeEmail.SelectedIndex = this.lstCCMeEmail.Items.IndexOf(this.lstCCMeEmail.Items.FindByValue(objFormInfo.EmailCcMeControl));
			
			objFormCtl.BindDynamicControls(this.lstEmailSendToControl, ModuleId);
			
			objFormCtl.BindDynamicControls(this.lstEmailSendFromControl, ModuleId);
			
			objFormCtl.BindDynamicControls(this.lstEmailReplyToControl, ModuleId);
			
			SetSendToControls();
			
			SetSendFromControls();
			
			SetReplyToControls();
			
		}
		
#endregion
		
		
#region Helper Methods
		
		private void SetSendToControls()
		{
			switch (this.lstEmailSendAction.SelectedItem.Value)
			{
				case "1": //None
					this.txtEmailSendTo.Enabled = false;
					this.txtEmailSendTo.Visible = true;
					this.lstEmailSendToControl.Visible = false;
					this.rfvEmailSendTo.Enabled = false;
					this.lblToEmailText.Visible = true;
					this.lblToEmailList.Visible = false;
					this.txtBCC.Enabled = false;
					break;
				case "2": //Admin
					this.txtEmailSendTo.Enabled = false;
					this.txtEmailSendTo.Visible = true;
					this.txtEmailSendTo.Text = this.PortalSettings.Email;
					this.lstEmailSendToControl.Visible = false;
					this.rfvEmailSendTo.Enabled = false;
					this.lblToEmailText.Visible = true;
					this.lblToEmailList.Visible = false;
					this.txtBCC.Enabled = true;
					break;
				case "3": //User Defined
					this.txtEmailSendTo.Enabled = true;
					this.txtEmailSendTo.Visible = true;
					this.lstEmailSendToControl.Visible = false;
					this.rfvEmailSendTo.Enabled = true;
					this.lblToEmailText.Visible = true;
					this.lblToEmailList.Visible = false;
					this.txtBCC.Enabled = true;
					break;
				case "4": //Use Form Value
					this.txtEmailSendTo.Visible = false;
					this.rfvEmailSendTo.Enabled = false;
					this.lstEmailSendToControl.Visible = true;
					this.lstEmailSendToControl.Enabled = true;
					this.lblToEmailText.Visible = false;
					this.lblToEmailList.Visible = true;
					this.txtBCC.Enabled = true;
					this.lstEmailSendToControl.SelectedIndex = this.lstEmailSendToControl.Items.IndexOf(this.lstEmailSendToControl.Items.FindByValue(objFormInfo.EmailSendToControl));
					break;
			}
		}
		
		private void SetSendFromControls()
		{
			switch (this.lstEmailFromAction.SelectedValue)
			{
				case "1": //Admin
					this.txtFrom.Enabled = false;
					this.txtFrom.Visible = true;
					this.txtFrom.Text = this.PortalSettings.Email;
					this.lstEmailSendFromControl.Visible = false;
					this.rfvFrom.Enabled = false;
					this.revFrom.Enabled = false;
					this.lstFromEmailText.Visible = true;
					this.lstFromEmailList.Visible = false;
					break;
				case "2": //User Defined
					this.txtFrom.Enabled = true;
					this.txtFrom.Visible = true;
					this.lstEmailSendFromControl.Visible = false;
					this.rfvFrom.Enabled = true;
					this.revFrom.Enabled = true;
					this.lstFromEmailText.Visible = true;
					this.lstFromEmailList.Visible = false;
					break;
				case "3": //Use a Control Value
					this.txtFrom.Visible = false;
					this.lstEmailSendFromControl.Visible = true;
					this.lstEmailSendFromControl.Enabled = true;
					this.rfvFrom.Enabled = false;
					this.revFrom.Enabled = false;
					this.lstFromEmailText.Visible = false;
					this.lstFromEmailList.Visible = true;
					this.lstEmailSendFromControl.SelectedIndex = this.lstEmailSendFromControl.Items.IndexOf(this.lstEmailSendFromControl.Items.FindByValue(objFormInfo.EmailSendFromControl));
					break;
			}
		}
		
		private void SetReplyToControls()
		{
			switch (this.lstReplyToAction.SelectedValue)
			{
				case "1": // None
					this.txtReplyTo.Enabled = false;
					this.txtReplyTo.Visible = true;
					this.lstEmailReplyToControl.Visible = false;
					this.lblReplyToEmailText.Visible = true;
					this.lblReplyToEmailList.Visible = false;
					this.rfvReplyTo.Enabled = false;
					this.revReplyTo.Enabled = false;
					break;
				case "2": //Admin
					this.txtReplyTo.Enabled = false;
					this.txtReplyTo.Visible = true;
					this.txtReplyTo.Text = this.PortalSettings.Email;
					this.lstEmailReplyToControl.Visible = false;
					this.lblReplyToEmailText.Visible = true;
					this.lblReplyToEmailList.Visible = false;
					this.rfvReplyTo.Enabled = false;
					this.revReplyTo.Enabled = false;
					break;
				case "3": //User Defined
					this.txtReplyTo.Enabled = true;
					this.txtReplyTo.Visible = true;
					this.lstEmailReplyToControl.Visible = false;
					this.lblReplyToEmailText.Visible = true;
					this.lblReplyToEmailList.Visible = false;
					this.rfvReplyTo.Enabled = true;
					this.revReplyTo.Enabled = true;
					break;
				case "4": //Use a Control Value
					this.txtReplyTo.Visible = false;
					this.lstEmailReplyToControl.Visible = true;
					this.lstEmailReplyToControl.Enabled = true;
					this.lblReplyToEmailText.Visible = false;
					this.lblReplyToEmailList.Visible = true;
					this.rfvReplyTo.Enabled = false;
					this.revReplyTo.Enabled = false;
					this.lstEmailReplyToControl.SelectedIndex = this.lstEmailReplyToControl.Items.IndexOf(this.lstEmailReplyToControl.Items.FindByValue(objFormInfo.ReplyToControl));
					break;
			}
		}
		
#endregion
		
		
#region Public Properties
		public PortalModuleBase LoaderCtl
		{
			get
			{
				if (_loaderCtl == null)
				{
					_loaderCtl = this;
				}
				
				return _loaderCtl;
			}
			set
			{
				_loaderCtl = value;
			}
		}
#endregion
		
		
	}
	
}


